package com.token.model.dto;

import com.token.service.Supplier;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.function.IntSupplier;
import java.util.stream.Collectors;

/**
 * @auther: henriy
 * @Date: 2023/12/29
 */
public class SupplierTest<T> {

    private final Supplier<List<T>> listSupplier;

    public SupplierTest(Supplier<List<T>> listSupplier) {
        this.listSupplier = listSupplier;
    }

    public List<T> getDeduplicatedList() {
        return listSupplier.get()
                .stream()
                .distinct()
                .collect(Collectors.toList());
    }

    public static void main(String[] args) {
        List<String> listWithDuplicates = Arrays.asList("A", "B", "A", "C", "B", "D");

        SupplierTest<String> listDeduplicator = new SupplierTest<>(() -> listWithDuplicates);
        List<String> listWithoutDuplicates = listDeduplicator.getDeduplicatedList();
        // 输出: [A, B, C, D]
        System.out.println(listWithoutDuplicates);

        Supplier<Integer> supplier = () -> new Random().nextInt(10);
        System.out.println(supplier.get());

        Supplier<LocalDateTime> supplier2 = LocalDateTime::now;
        System.out.println(supplier2.get());

        Dog dog1 = dogFactory(() -> new Dog("哈士奇"));
        Dog dog2 = dogFactory(() -> new Dog("牧羊犬"));
        System.out.println(dog1);
        System.out.println(dog2);

        // 为了方便 Supplier 的使用，提供了指定类型的 Supplier，有 BooleanSupplier, DoubleSupplier, IntSupplier, LongSupplier。
        IntSupplier intSupplier = () -> new Random().nextInt(10);
        System.out.println(intSupplier.getAsInt());
    }

    public static Dog dogFactory(Supplier<? extends Dog> supplier) {
        Dog dog = supplier.get();
        dog.setAge(1);
        return dog;
    }

}
